package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.pojo.KUser;
import com.example.service.IKUserService;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2023年09月09日 18:51:00
 * @Description
 */
@RestController
public class KUserController {

    @Autowired
    private IKUserService userService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 添加或者修改用户
     */
    @PostMapping("/user/saveupdate")
    public KUser saveOrUpdateUser(@RequestBody KUser kUser){
        KUser kUsers = userService.saveOrUpdateUser(kUser);
        return kUsers;
    }

    /**
     * 根据id删除用户信息
     */
    @PostMapping("/user/delete/{userId}")
    public Integer saveOrUpdateUser(@PathVariable("userId") Long userId){
        Integer result = userService.deleleUserByUserId(userId);
        return result;
    }

    /**
     * 查询用户列表信息
     * @return
     */
    @GetMapping("/user/list")
    public List<KUser> findUserList(){
        List<KUser> kUsers = userService.selelctUserList();
        return kUsers;
    }


    /**
     * 分页查询用户列表信息
     * @return
     */
    @GetMapping("/user/page")
    public IPage<KUser> findUsers(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        IPage<KUser> kUserIPage = userService.selelctUserPages(pageNo, pageSize);
        return kUserIPage;
    }

    /**
     * 根据用户id查询用户明细
     */
    @GetMapping("/user/get/{userId}")
    public KUser getUserByUserId(@PathVariable("userId") Long userId){
        KUser kUser = userService.selectUserByUserId(userId);
        return kUser;
    }

    @PostMapping("/create/elastic/index")
    public String createElasticIndex() throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("users");
        createIndexRequest.mapping("{\n" +
                "    \"properties\": {\n" +
                "      \"password\": {\n" +
                "        \"type\": \"keyword\"   \n" +
                "      },\n" +
                "      \"update_time\": {\n" +
                "        \"type\": \"date\",\n" +
                "        \"format\": \"yyyy-MM-dd HH:mm:ss\"\n" +
                "      },\n" +
                "      \"create_time\": {\n" +
                "        \"type\": \"date\",\n" +
                "        \"format\": \"yyyy-MM-dd HH:mm:ss\"\n" +
                "      },\n" +
                "      \"address\": {\n" +
                "        \"type\": \"text\"\n" +
                "      },\n" +
                "      \"phone\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"sex\": {\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"id\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"username\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"hobby\": {\n" +
                "        \"type\": \"text\"\n" +
                "      }\n" +
                "    }\n" +
                "  }",XContentType.JSON);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged());
        restHighLevelClient.close();
        return "success";
    }

}
